/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.project.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import io.renren.modules.project.entity.BackendEntity;
import io.renren.modules.project.entity.ModuleEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Mapper
public interface ModuleDao extends BaseMapper<ModuleEntity> {
    @Select("select m.*,b.name as backend_name,scs.name as server_name,scs.ip_addr as server_ipaddr " +
            ",dsdb.name as database_name" +
            ",dsmongo.name as mongodb_name" +
            ",dsredis.name as redis_name" +
            "from module m " +
            "left join backend b on m.backend_id = b.id " +
            "left join sys_config_server scs on m.server_id = scs.id " +
            "left join datasource dsdb on m.database_id = dsdb.id  " +
            "left join datasource dsmongo on m.mongodb_id = dsmongo.id  " +
            "left join datasource dsredis on m.redis_id = dsredis.id  " +
            "where f.name like #{key}")
    ModuleEntity queryByKey(String key);
    @Select("select m.*,b.name as backend_name,scs.name as server_name,scs.ip_addr as server_ipaddr" +
            ",dsdb.name as database_name" +
            ",dsmongo.name as mongodb_name" +
            ",dsredis.name as redis_name" +
            " from module m " +
            "left join backend b on m.backend_id = b.id " +
            "left join sys_config_server scs on m.server_id = scs.id  " +
            "left join datasource dsdb on m.database_id = dsdb.id  " +
            "left join datasource dsmongo on m.mongodb_id = dsmongo.id  " +
            "left join datasource dsredis on m.redis_id = dsredis.id  " +
            "limit ${size},${limit} ")
    List<ModuleEntity> selectEntityPage(String key, long size, long limit);
    @Select("select count(1) from module")
    long selectRecordSize(String key);
    @Select("select m.*,b.name as backend_name,scs.name as server_name,scs.ip_addr as server_ipaddr" +
            ",dsdb.name as database_name" +
            ",dsmongo.name as mongodb_name" +
            ",dsredis.name as redis_name" +
            " from module m " +
            "left join backend b on m.backend_id = b.id " +
            "left join sys_config_server scs on m.server_id = scs.id  " +
            "left join datasource dsdb on m.database_id = dsdb.id  " +
            "left join datasource dsmongo on m.mongodb_id = dsmongo.id  " +
            "left join datasource dsredis on m.redis_id = dsredis.id  " +
            "where id = #{moduleId} ")
    ModuleEntity getAllById(long moduleId);
    @Select("select DISTINCT ds.*,m.db_name,scs.name as serverName,scs.ip_addr as serverIpaddr from  backend bd left join module m on m.backend_id = bd.id left join datasource ds on (ds.id= m.database_id or ds.id= m.mongodb_id or ds.id= m.redis_id)  left join sys_config_server scs on ds.server_id = scs.id where bd.project_id = #{projectId}  and ds.id= #{datasourceId}")
    List<HashMap> selectModuleDatasourceListByProjectIdAndDatasourceId(long projectId, long datasourceId);
}
